home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing;
-
- import com.sun.java.swing.event.EventListenerList;
- import com.sun.java.swing.event.ListSelectionEvent;
- import com.sun.java.swing.event.ListSelectionListener;
- import java.io.Serializable;
- import java.util.BitSet;
-
- public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable {
- private int selectionMode = 2;
- private int minIndex = -1;
- private int maxIndex = -1;
- private int anchorIndex = -1;
- private int leadIndex = -1;
- private int firstChangedIndex = -1;
- private int lastChangedIndex = -1;
- private boolean isAdjusting = false;
- protected BitSet value = new BitSet(32);
- protected EventListenerList listenerList = new EventListenerList();
- protected boolean leadAnchorNotificationEnabled = true;
- static Class class$com$sun$java$swing$event$ListSelectionListener;
-
- public void addListSelectionListener(ListSelectionListener l) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$ListSelectionListener = var10001;
- }
-
- var10000.add(var10001, l);
- }
-
- public void addSelectionInterval(int index0, int index1) {
- int mode = this.getSelectionMode();
- if (mode != 0 && mode != 1) {
- if (index0 != -1 || index1 != -1) {
- int addMinIndex = Math.min(index0, index1);
- int addMaxIndex = Math.max(index0, index1);
- if (this.minIndex == -1 || addMinIndex < this.minIndex) {
- this.minIndex = addMinIndex;
- }
-
- if (this.maxIndex == -1 || addMaxIndex > this.maxIndex) {
- this.maxIndex = addMaxIndex;
- }
-
- this.firstChangedIndex = -1;
- this.lastChangedIndex = -1;
-
- for(int r = addMinIndex; r <= addMaxIndex; ++r) {
- if (!this.value.get(r)) {
- if (this.firstChangedIndex == -1) {
- this.firstChangedIndex = this.lastChangedIndex = r;
- } else if (r > this.lastChangedIndex) {
- this.lastChangedIndex = r;
- }
-
- this.value.set(r);
- }
- }
-
- this.updateLeadAnchorIndices(index0, index1);
- if (this.firstChangedIndex != -1 && this.lastChangedIndex != -1) {
- this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
- }
-
- }
- } else {
- this.setSelectionInterval(index0, index1);
- }
- }
-
- public void clearSelection() {
- if (this.minIndex != -1 && this.maxIndex != -1) {
- boolean selectionChanged = false;
-
- for(int r = this.minIndex; r <= this.maxIndex; ++r) {
- if (this.value.get(r)) {
- selectionChanged = true;
- }
-
- this.value.clear(r);
- }
-
- this.minIndex = this.maxIndex = this.anchorIndex = this.leadIndex = -1;
- if (selectionChanged) {
- this.fireValueChanged(this.minIndex, this.maxIndex);
- }
-
- }
- }
-
- public Object clone() throws CloneNotSupportedException {
- DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();
- clone.value = (BitSet)this.value.clone();
- clone.listenerList = new EventListenerList();
- return clone;
- }
-
- private boolean contains(int a, int b, int index) {
- if (a <= b) {
- return index >= a && index < b;
- } else {
- return index <= a && index > b;
- }
- }
-
- protected void fireValueChanged(int firstIndex, int lastIndex) {
- this.fireValueChanged(firstIndex, lastIndex, this.getValueIsAdjusting());
- }
-
- protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) {
- Object[] listeners = this.listenerList.getListenerList();
- ListSelectionEvent e = null;
-
- for(int i = listeners.length - 2; i >= 0; i -= 2) {
- Object var10000 = listeners[i];
- Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
- } catch (ClassNotFoundException var7) {
- throw new NoClassDefFoundError(((Throwable)var7).getMessage());
- }
-
- class$com$sun$java$swing$event$ListSelectionListener = var10001;
- }
-
- if (var10000 == var10001) {
- if (e == null) {
- e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
- }
-
- ((ListSelectionListener)listeners[i + 1]).valueChanged(e);
- }
- }
-
- }
-
- protected void fireValueChanged(boolean isAdjusting) {
- this.fireValueChanged(this.getMinSelectionIndex(), this.getMaxSelectionIndex(), isAdjusting);
- }
-
- public int getAnchorSelectionIndex() {
- return this.anchorIndex;
- }
-
- public int getLeadSelectionIndex() {
- return this.leadIndex;
- }
-
- public int getMaxSelectionIndex() {
- return this.maxIndex;
- }
-
- public int getMinSelectionIndex() {
- return this.minIndex;
- }
-
- public int getSelectionMode() {
- return this.selectionMode;
- }
-
- public boolean getValueIsAdjusting() {
- return this.isAdjusting;
- }
-
- public void insertIndexInterval(int index, int length, boolean before) {
- int insMinIndex = before ? Math.max(0, index - 1) : index + 1;
- int insMaxIndex = insMinIndex + length - 1;
- boolean selected = this.value.get(index) && this.value.get(insMinIndex);
-
- for(int i = this.maxIndex; i >= insMinIndex; --i) {
- if (this.value.get(i)) {
- this.value.set(i + length);
- } else {
- this.value.clear(i + length);
- }
- }
-
- for(int i = insMinIndex; i <= insMaxIndex; ++i) {
- if (selected) {
- this.value.set(i);
- } else {
- this.value.clear(i);
- }
- }
-
- if (this.minIndex != -1 && (this.minIndex > insMinIndex || this.minIndex == insMinIndex && !selected)) {
- this.minIndex += length;
- }
-
- if (this.maxIndex != -1 && this.maxIndex >= insMinIndex) {
- this.maxIndex += length;
- }
-
- this.fireValueChanged(insMinIndex, this.maxIndex);
- }
-
- public boolean isLeadAnchorNotificationEnabled() {
- return this.leadAnchorNotificationEnabled;
- }
-
- public boolean isSelectedIndex(int index) {
- return index != -1 && index >= this.minIndex && index <= this.maxIndex ? this.value.get(index) : false;
- }
-
- public boolean isSelectionEmpty() {
- return this.minIndex == -1 && this.maxIndex == -1;
- }
-
- public void removeIndexInterval(int index0, int index1) {
- if (index0 >= 0 && index1 >= 0) {
- if (this.minIndex != -1 || this.maxIndex != -1) {
- int rmMinIndex = Math.min(index0, index1);
- int rmMaxIndex = Math.max(index0, index1);
- int gapLength = rmMaxIndex - rmMinIndex + 1;
- if (rmMinIndex <= this.maxIndex && rmMaxIndex >= this.minIndex) {
- if (rmMinIndex <= this.minIndex && rmMaxIndex >= this.maxIndex) {
- this.clearSelection();
- } else {
- for(int i = rmMinIndex; i <= rmMaxIndex; ++i) {
- if (this.value.get(i + gapLength)) {
- this.value.set(i);
- } else {
- this.value.clear(i);
- }
- }
-
- for(int i = this.maxIndex; i > this.maxIndex - gapLength; --i) {
- this.value.clear(i);
- }
-
- int oldMinIndex = this.minIndex;
- int oldMaxIndex = this.maxIndex;
- if (oldMinIndex >= rmMinIndex) {
- this.minIndex = -1;
-
- for(int i = rmMinIndex; i <= oldMaxIndex; ++i) {
- if (this.value.get(i)) {
- this.minIndex = i;
- break;
- }
- }
- }
-
- if (this.minIndex == -1) {
- this.maxIndex = -1;
- } else if (oldMaxIndex > rmMaxIndex) {
- this.maxIndex = oldMaxIndex - gapLength;
- } else {
- for(int i = oldMaxIndex; i >= this.minIndex; --i) {
- if (this.value.get(i)) {
- this.maxIndex = i;
- break;
- }
- }
- }
-
- if (this.maxIndex == -1 && this.minIndex == -1) {
- this.fireValueChanged(rmMinIndex, rmMaxIndex);
- } else {
- this.firstChangedIndex = Math.min(rmMinIndex, this.minIndex);
- this.lastChangedIndex = Math.max(rmMaxIndex, this.maxIndex);
- this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
- }
-
- }
- }
- }
- }
- }
-
- public void removeListSelectionListener(ListSelectionListener l) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$ListSelectionListener = var10001;
- }
-
- var10000.remove(var10001, l);
- }
-
- public void removeSelectionInterval(int index0, int index1) {
- if (index0 != -1 || index1 != -1) {
- if (this.minIndex != -1 || this.maxIndex != -1) {
- int remMinIndex = Math.min(index0, index1);
- int remMaxIndex = Math.max(index0, index1);
- int firstChangedIndex = -1;
- int lastChangedIndex = -1;
-
- for(int r = remMinIndex; r <= remMaxIndex; ++r) {
- if (this.value.get(r)) {
- if (firstChangedIndex == -1) {
- lastChangedIndex = r;
- firstChangedIndex = r;
- } else if (r > lastChangedIndex) {
- lastChangedIndex = r;
- }
-
- this.value.clear(r);
- }
- }
-
- int newMinIndex = -1;
- int newMaxIndex = -1;
-
- for(int r = this.minIndex; r <= this.maxIndex; ++r) {
- if (this.value.get(r)) {
- if (newMinIndex == -1) {
- newMaxIndex = r;
- newMinIndex = r;
- } else if (r > newMaxIndex) {
- newMaxIndex = r;
- }
- }
- }
-
- this.minIndex = newMinIndex;
- this.maxIndex = newMaxIndex;
- if (firstChangedIndex != -1 && lastChangedIndex != -1) {
- this.fireValueChanged(firstChangedIndex, lastChangedIndex);
- }
-
- }
- }
- }
-
- public void setAnchorSelectionIndex(int index) {
- this.anchorIndex = index;
- }
-
- public void setLeadAnchorNotificationEnabled(boolean flag) {
- this.leadAnchorNotificationEnabled = flag;
- }
-
- public void setLeadSelectionIndex(int index) {
- this.updateLead(index);
- this.leadIndex = index;
- }
-
- public void setSelectionInterval(int index0, int index1) {
- if (this.getSelectionMode() == 0) {
- index0 = index1;
- }
-
- if (index0 == -1 && index1 == -1) {
- this.clearSelection();
- } else {
- int newMinIndex = Math.min(index0, index1);
- int newMaxIndex = Math.max(index0, index1);
- if (this.minIndex == -1 || newMinIndex < this.minIndex) {
- this.minIndex = newMinIndex;
- }
-
- if (this.maxIndex == -1 || newMaxIndex > this.maxIndex) {
- this.maxIndex = newMaxIndex;
- }
-
- this.firstChangedIndex = -1;
- this.lastChangedIndex = -1;
-
- for(int r = this.minIndex; r < newMinIndex; ++r) {
- if (this.value.get(r)) {
- if (this.firstChangedIndex == -1) {
- this.firstChangedIndex = this.lastChangedIndex = r;
- } else if (r > this.lastChangedIndex) {
- this.lastChangedIndex = r;
- }
- }
-
- this.value.clear(r);
- }
-
- for(int r = newMinIndex; r <= newMaxIndex; ++r) {
- if (!this.value.get(r)) {
- if (this.firstChangedIndex == -1) {
- this.firstChangedIndex = this.lastChangedIndex = r;
- } else if (r > this.lastChangedIndex) {
- this.lastChangedIndex = r;
- }
- }
-
- this.value.set(r);
- }
-
- for(int r = newMaxIndex + 1; r <= this.maxIndex; ++r) {
- if (this.value.get(r)) {
- if (this.firstChangedIndex == -1) {
- this.firstChangedIndex = this.lastChangedIndex = r;
- } else if (r > this.lastChangedIndex) {
- this.lastChangedIndex = r;
- }
- }
-
- this.value.clear(r);
- }
-
- this.updateLeadAnchorIndices(index0, index1);
- this.minIndex = newMinIndex;
- this.maxIndex = newMaxIndex;
- if (this.firstChangedIndex != -1 && this.lastChangedIndex != -1) {
- this.fireValueChanged(this.firstChangedIndex, this.lastChangedIndex);
- }
-
- }
- }
-
- public void setSelectionMode(int selectionMode) {
- switch (selectionMode) {
- case 0:
- case 1:
- case 2:
- this.selectionMode = selectionMode;
- return;
- default:
- throw new IllegalArgumentException("invalid selectionMode");
- }
- }
-
- public void setValueIsAdjusting(boolean b) {
- if (b != this.isAdjusting) {
- this.isAdjusting = b;
- this.fireValueChanged(b);
- }
-
- }
-
- private int sign(int a, int b) {
- return a < b ? -1 : (a > b ? 1 : 0);
- }
-
- public String toString() {
- String s = (this.getValueIsAdjusting() ? "~" : "=") + this.value.toString();
- return this.getClass().getName() + " " + Integer.toString(this.hashCode()) + " " + s;
- }
-
- private void updateLead(int index) {
- if (this.anchorIndex == -1) {
- this.anchorIndex = index;
- }
-
- boolean deselect = false;
- if (!deselect && this.contains(this.leadIndex, index, this.anchorIndex)) {
- this.removeSelectionInterval(this.anchorIndex, this.leadIndex);
- this.addSelectionInterval(this.anchorIndex, index);
- }
-
- if (!deselect && !this.contains(this.anchorIndex, this.leadIndex, index)) {
- this.addSelectionInterval(this.anchorIndex, index);
- } else {
- int delta = this.sign(this.anchorIndex, this.leadIndex);
- this.removeSelectionInterval(index - delta, this.leadIndex);
- }
-
- }
-
- private void updateLeadAnchorIndices(int index0, int index1) {
- if (this.leadAnchorNotificationEnabled) {
- if (this.anchorIndex != -1 && this.anchorIndex != index0) {
- int minAnchorIndex = Math.min(this.anchorIndex, index0);
- int maxAnchorIndex = Math.max(this.anchorIndex, index0);
- this.firstChangedIndex = this.firstChangedIndex == -1 ? maxAnchorIndex : Math.min(minAnchorIndex, this.firstChangedIndex);
- this.lastChangedIndex = Math.max(maxAnchorIndex, this.lastChangedIndex);
- }
-
- if (this.leadIndex != -1 && this.leadIndex != index1) {
- int minLeadIndex = Math.min(this.leadIndex, index1);
- int maxLeadIndex = Math.max(this.leadIndex, index1);
- this.firstChangedIndex = this.firstChangedIndex == -1 ? minLeadIndex : Math.min(minLeadIndex, this.firstChangedIndex);
- this.lastChangedIndex = Math.max(maxLeadIndex, this.lastChangedIndex);
- }
- }
-
- this.anchorIndex = index0;
- this.leadIndex = index1;
- }
- }
-